package com.house365.core.view;

import com.house365.core.constant.CorePreferences;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

/**
 *@author py
 *@date 2012-11-27
 *@comment ScrollView 横向滑动优先
 */
public class ScrollViewHorzFling extends ScrollView {
    private boolean canScroll;
 
    private GestureDetector mGestureDetector;
    View.OnTouchListener mGestureListener;
 
    public ScrollViewHorzFling(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(new YScrollDetector());
        canScroll = true;
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(ev.getAction() == MotionEvent.ACTION_UP ||ev.getAction() == MotionEvent.ACTION_DOWN){
            canScroll = true;
        }
        boolean t = mGestureDetector.onTouchEvent(ev);
        CorePreferences.DEBUG(t+"");
        return super.onInterceptTouchEvent(ev) && t;
    }
 
    class YScrollDetector extends SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if(canScroll){
                canScroll = Math.abs(distanceY) > Math.abs(distanceX);
                CorePreferences.ERROR(Math.abs(distanceY)+","+Math.abs(distanceX));
            }
            CorePreferences.ERROR(canScroll+":canScroll");
            return canScroll;
        }
    }
}
